package com.hfxt.controller;

import com.hfxt.config.UserConfig;
import com.hfxt.constants.ResultEnum;
import com.hfxt.dto.BaseException;
import com.hfxt.dto.Result;
import com.hfxt.po.Brand;
import com.hfxt.service.BrandService;
import com.hfxt.utils.ResultUtils;
import com.hfxt.utils.SmsUtils;
import com.hfxt.utils.StringUtil;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@PropertySource(value = {"classpath:user.yml"})
public class TestController {

    @Value("${uname}")
    private String uname;
    @Autowired
    private UserConfig userConfig;
    @Value("${name}")
    private String name;


    @Resource
    private BrandService brandService;

    @RequestMapping("/index")
    public String index(){
        return "hello world";
    }

    @RequestMapping("/query")
    public List<Brand> queryBrandList(){
        Map<String, Object> maps=new HashMap<String, Object>();
        return brandService.getBrandListByMap(maps);
    }

    //统一返回数据包装类
    @RequestMapping("/testResult")
    public Result testResult(){
        Map<String, Object> map= StringUtil.createSimpleMap("name", "bdqn");
        //Result result= ResultUtils.returnSuccess("成功", map);

        Result result= ResultUtils.returnFail(ResultEnum.FAIL_HAVE_DELETED.getCode(),ResultEnum.FAIL_HAVE_DELETED.getMsg());
        return result;
    }


    @RequestMapping("/testException")
    public Result testException() throws Exception{
       // int a=1/0;
       // return ResultUtils.returnSuccess();

        String name="bdqn";
        if("bdqn".equals(name)){
            throw new BaseException(ResultEnum.FAIL_ACCOUNT_LOCK);
        }
        return ResultUtils.returnSuccess();
    }

    @RequestMapping("/testProp")
    public Result testProp() {
        System.out.println(uname);
        System.out.println(userConfig.getUsername() + "---" + userConfig.getPassword());
        System.out.println(name);
        return ResultUtils.returnSuccess();
    }

   /* @RequestMapping("/testSms")
    public String testSms(){
        return SmsUtils.
    }
*/
}
